﻿//
// Created by Administrator on 2022/6/16.
//

#include "MyDialog.h"
#include "action_event.h"
#include "ids.h"

#define TIMER_ID 1000

BEGIN_EVENT_TABLE ( MyDialog, wxFrame )
                EVT_MENU ( ACTION_EVENT, MyDialog::OnActionEvent )
                EVT_TIMER( TIMER_ID, MyDialog::OnTimer )
END_EVENT_TABLE()

class DelayThread: public wxThread
{
    wxWindow * parent;
    wxString *s;
public:
    explicit DelayThread ( wxWindow * parent ){
        this->parent = parent;
        s = new wxString("");
    }
    void * Entry() override{
        while ( true ) {
            Sleep ( 1000 );
            s->append("1");
            ActionEvent event ( this->parent, TOKEN_DELAY_TEST, s );
            event.Post();
        }
    }
    ~DelayThread(){
        delete s;
    }
};

MyDialog::MyDialog( wxWindow *parent )
    :wxFrame ( parent, ID_FRAME_REPOSITORY, "Transfer", wxDefaultPosition, wxDefaultSize,
                       wxDEFAULT_FRAME_STYLE ^ wxMAXIMIZE_BOX ^ wxRESIZE_BORDER )
{
    CreateStatusBar();
    SetLabel("Transfer");
    SetClientSize ( wxSize ( 350, 500 ) );
    auto* dialogRootBoxSizer = new wxBoxSizer ( wxVERTICAL );
    auto *start_post_button = new wxButton ( this, ID_BUTTON_START_POST, "启动传送" );
    dialogRootBoxSizer->Add( start_post_button );
    this->SetSizer ( dialogRootBoxSizer );
    //start the thread
    mThread = new DelayThread( this );
    mThread->Run();
}

MyDialog::~MyDialog(){

}

void MyDialog::OnActionEvent( wxCommandEvent &event ){
    const int token = event.GetInt();
    const long liType = event.GetExtraLong();
    switch ( token ) {
        case TOKEN_INFO:
            break;
        case TOKEN_ERROR:
            break;
        case TOKEN_ACTION_START:
            break;
        case TOKEN_ACTION_END:
            break;
        case TOKEN_DELAY_TEST: {
            wxString *s = (wxString*)event.GetClientData();
            wxMessageBox( (* s ));
            break;
        }
        case TOKEN_LOG:{
            break;
        }
        case TOKEN_SCP_PERCENT:{
            break;
        }
    }
}

void MyDialog::OnTimer(wxTimerEvent &event) {

}